package com.sl.config;


import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.jms.ConnectionFactory;
import javax.jms.Topic;

import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.transaction.jta.JtaTransactionManager;

/**
 * activemq配置类
 * @author lunshi
 * TODO
 * @创建时间 2020 2020年3月8日 下午2:55:05
 * @email:shilun217@qq.com
 */
@Configuration
public class ActiveMQBeanConfig{
	
	@Value("${spring.activemq.consumer.queue.threadNum}")
	String queueThreadNum;
	@Value("${spring.activemq.consumer.topic.threadNum}")
	String topcThreadNum;
	 
	@Bean(name = "testQueue")
	public ActiveMQQueue reqLogQueue(){
		//设置次消息队列预取消息条数consumer.prefetchSize=2
		return new ActiveMQQueue("test.queue?consumer.prefetchSize=2");
	}
	
	@Bean(name="testTopic") 
	public Topic topic(){
		return new ActiveMQTopic("test.topic");
	}
	
	/**
	 * topic监听器对应的containerFactory
	 * @Date 2020年3月8日 下午3:12:54 lunshi
	 * @param activeMQConnectionFactory
	 * @return JmsListenerContainerFactory<?>
	 */
	@Bean
	public JmsListenerContainerFactory<?> jmsListenerContainerTopic(ConnectionFactory activeMQConnectionFactory) {
		DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
		bean.setPubSubDomain(true);
		bean.setConnectionFactory(activeMQConnectionFactory);
		ExecutorService topic = null;Executors.newFixedThreadPool(Integer.valueOf(topcThreadNum), new CustomThreadFactory("topic"));
		bean.setTaskExecutor(topic);
		bean.setConcurrency(topcThreadNum);
		//bean.setTransactionManager(jtaTransactionManager);
		return bean;
	}
	
	@Bean
	public JmsListenerContainerFactory<?> jmsListenerContainerQueue(ConnectionFactory activeMQConnectionFactory) {
		DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
		bean.setPubSubDomain(false);
		bean.setConnectionFactory(activeMQConnectionFactory);
		ExecutorService queue =null; Executors.newFixedThreadPool(Integer.valueOf(queueThreadNum), new CustomThreadFactory("queue"));
		bean.setTaskExecutor(queue);
		bean.setConcurrency(queueThreadNum);
		//bean.setTransactionManager(jtaTransactionManager);
		return bean;
	}
}
